package com.xiaohai.wx.weather.message;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xiaohai.wx.weather.service.LocationService;
import com.xiaohai.wx.weather.service.UserService;
import com.xiaohai.wx.weather.util.MessageUtil;

@Service("EventMsgHandler")
public class EventMsgHandler {

	@Autowired
	SessionHandler sessionHandler;

	@Autowired
	UserService userService;
	
	@Autowired
	LocationService locationService;

	public String handleEvent(String eventType, String user_OpenId) {

		String respContent = "";

		if (eventType.equals(MessageUtil.EVENT_TYPE_SUBSCRIBE)) {
			
			String cityNames = locationService.getRandomLocationStr();

			userService.addNewUser(user_OpenId, cityNames);

			respContent = "谢谢您的关注！请问您想订阅哪些城市的天气预报呢？多个城市请用逗号分隔，例如: XXXX.";
			
			respContent = respContent.replace("XXXX", cityNames);
			sessionHandler.setTopic(user_OpenId, 
					SessionHandler.TOPIC_QUERY_LOCATION);
			
		} else if (eventType.equals(MessageUtil.EVENT_TYPE_UNSUBSCRIBE)) {
			userService.removeConsumerFromDB(user_OpenId);
		}

		return respContent;
	}
}
